package com.hxht.storage.server.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hxht.storage.server.bean.IpchannelBean;
import com.hxht.storage.server.bean.NvrDeviceBean;
import com.hxht.storage.server.bean.NvrDiskBean;
import com.hxht.storage.server.bean.NvrRegisteBean;
import com.hxht.storage.server.bean.NvrUpdateBean;
import com.hxht.storage.server.pojo.NvrDeviceInfo;
import com.hxht.storage.server.pojo.NvrDiskInfo;
import com.hxht.storage.server.pojo.NvrIpchannelInfo;
import com.hxht.storage.server.service.NvrDeviceService;
import com.hxht.storage.server.util.NvrBeanTransUtil;

@Controller
@RequestMapping("/nvrCtrl/")
public class NvrController {
	
	private static final Logger LOG = Logger.getLogger(NvrController.class);

	@Autowired
	private NvrDeviceService nvrService;
	
	@RequestMapping(value= "/", method = RequestMethod.POST)
	@ResponseBody
	public boolean registerNvr(@RequestBody NvrRegisteBean nvr){
		
//		response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
		
		if (nvrService.registerNvrDevice(nvr)){
			return true;
		} else {
			return false;
		}
		
	}

	/**
	 * 获得当前存储服务器管辖的所有NVR基本状态信息
	 * @return
	 */
	@RequestMapping(value= "/", method = RequestMethod.GET)
	@ResponseBody
	public NvrDeviceBean getAllNvr(){
		
		NvrDeviceBean device = new NvrDeviceBean();
		device.setDeviceId("1234");
		device.setIp("10.33.138.17");
		
		return device;
		
//		List<NvrDeviceInfo> nvrList = nvrService.getAllNvr();
//		
//		List<NvrDeviceBean> beanList = new ArrayList<NvrDeviceBean>();
//		for (NvrDeviceInfo nvr:nvrList){
//			NvrDeviceBean bean = transNvrInfoToNvrBean(nvr);
//			beanList.add(bean);
//		}
//		
//		return beanList;
	}
	
	private NvrDeviceBean transNvrInfoToNvrBean(NvrDeviceInfo nvr) {
		NvrDeviceBean bean = new NvrDeviceBean();
		bean.setDeviceId(nvr.getDeviceId());
		bean.setDeviceType(nvr.getDeviceType());
		bean.setIp(nvr.getIp());
		bean.setMaxIpchannels(nvr.getMaxIpchannels());
		bean.setOnline(nvr.isOnline());
		bean.setPasswd(nvr.getPasswd());
		bean.setPort(nvr.getPort());
		bean.setSubDeviceType(nvr.getSubDeviceType());
		bean.setUsername(nvr.getUsername());
		
		List<NvrDiskBean> diskList = new ArrayList<NvrDiskBean>();
		for (NvrDiskInfo disk:nvr.getDiskList()){
			NvrDiskBean diskBean = new NvrDiskBean();
			diskBean.setCapacity(disk.getCapacity());
			diskBean.setFreePictureCapacity(disk.getFreePictureCapacity());
			diskBean.setFreeSpace(disk.getFreeSpace());
			diskBean.setPictureCapacity(disk.getPictureCapacity());
			diskBean.setDiskIndex(disk.getDiskIndex());
			
			diskList.add(diskBean);
		}
		bean.setDiskList(diskList);

		return bean;
	}

	/**
	 * 根据nvrIp，找到对应的该nvr的ip通道
	 * @param nvrIp
	 * @return
	 */
	@RequestMapping(value= "/", method = RequestMethod.GET, 
			params = {"nvrIp"})
	@ResponseBody
	public List<IpchannelBean> getNvrStatus(@RequestParam String nvrIp){
		List<NvrIpchannelInfo>  ipchannelList = nvrService.getIpchannelOfNvr(nvrIp);
		
		if (ipchannelList == null || ipchannelList.size() == 0){
			LOG.warn("can't get nvr's ipchannels! nvrIp = " + nvrIp);
			return null;
		}
		
		List<IpchannelBean> beanList = new ArrayList<IpchannelBean>();
		for (NvrIpchannelInfo ipchannel:ipchannelList){
			IpchannelBean bean = transIpchannelInfoToIpchannelBean(ipchannel);
			beanList.add(bean);
		}
		
		return beanList;
	}

	private IpchannelBean transIpchannelInfoToIpchannelBean(
			NvrIpchannelInfo ipchannel) {
		
		IpchannelBean bean = new IpchannelBean();
		bean.setDvrChannel(ipchannel.getNvrChannel());
		bean.setIsIdle(ipchannel.isIdle());
		if (!ipchannel.isIdle()){
			bean.setDeviceChannel(ipchannel.getDeviceChannel());
			bean.setIp(ipchannel.getIp());
			bean.setPort(ipchannel.getPort());
			bean.setProtocol(NvrBeanTransUtil.
					getIpchannelProtocol(ipchannel.getProtocol()));
			bean.setIsOnline(ipchannel.isOnline());
		}
		
		return bean;
	}
	
	/**
	 * 根据nvr的 SN号，找到相应nvr,并更新nvr基本信息
	 * @param nvr
	 */
	@RequestMapping(value= "/", method = RequestMethod.PUT)
	public void updateNvr(@RequestBody NvrUpdateBean nvr){
		
		nvrService.updateNvr(nvr);
	}
	
	/**
	 * 根据nvr的SN号，删除该nvr
	 */
	@RequestMapping(value= "/", method = RequestMethod.DELETE)
	public void deleteNvr(@RequestParam String sn){
		
		nvrService.deleteNvr(sn);
	}
	
	/**
	 * 初始化NVR<br/>
	 * 将NVR的 所有ip通道相关的录像进行停止，然后将所有ip通道解除绑定，恢复空闲状态
	 */
	@RequestMapping(value="/init")
	public boolean initNvr(@RequestParam String sn){
		
		return nvrService.initScanIpchannel(sn);
	}
	
	/** 根据传入的 nvr sn号、磁盘序号，格式化相应磁盘 <br/>
	 * */
	@RequestMapping(value="/format")
	public boolean formatDisk(@RequestParam String sn, @RequestParam int diskIndex){
		
		return nvrService.formatDisk(sn, diskIndex);
	}
}
